//
// Created by 86131 on 2023/9/16.
//
#include <iostream>
    using namespace std;
    typedef int ElementType;
    typedef int Position;
    typedef struct SNode *PtrToSNode;
    struct SNode {
        ElementType *Data;//存储数组元素的数组
        Position Top;//栈顶元素指针
        int MaxSize;//栈堆最大容量
        int lenght;//哨兵记录栈的长度
    };
    typedef PtrToSNode Stack;

    Stack CreateStack(int MaxSize) {//初始化堆
        Stack S = (Stack) malloc(sizeof(struct SNode));
        S->Data = (ElementType * )
        ::malloc((MaxSize * sizeof(ElementType)));//给这个堆的data分配最大项数乘以每一项的空间
        S->Top = -1;//负一代表没有元素
        S->MaxSize = MaxSize;
       // S->lenght=0;
        return S;
    }

//入栈Push
    bool IsFull(Stack S) {//判断栈是否满
        return (S->Top == S->MaxSize - 1);
    }

    bool Push(Stack S, ElementType X) {
        if (IsFull(S)) {
            printf("栈堆满");
            return false;//入站失败
        } else {
            S->Data[++(S->Top)] = X;//将栈堆++，并将元素压入堆
            return true;//入栈成功
        }
    }

//出栈Pop+
    bool IsEmpty(Stack S) {//判断栈是否空
        return (S->Top == -1);
    }

    ElementType Pop(Stack S) {
        if (IsEmpty(S)) {
            printf("栈堆空");
            return 0;
        } else
            return (S->Data[(S->Top)--]);//返回出去之后，在对S-Top减一
    }